package test

import (
	"context"
	"fmt"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func GetMongo() (*mongo.Database, error) {
	credential := options.Credential{
		AuthMechanism: "SCRAM-SHA-256",
		Username:      "admin",
		Password:      "lsjweiyiMongo123.",
		AuthSource:    "admin",
	}
	clientOpts := options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d", "120.25.238.155", 27017)).SetAuth(credential)
	var ctx = context.TODO()
	client, err := mongo.Connect(ctx, clientOpts)
	if err != nil {
		return nil, err
	}

	// Check the connection
	err = client.Ping(ctx, nil)
	if err != nil {
		return nil, err
	}

	mongodb := client.Database("test")

	return mongodb, nil
}
